package viroyal.com.base.widget;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.suntiago.baseui.utils.ScreenUtils;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import viroyal.com.base.R;
import viroyal.com.base.model.Announced;


/**
 * @author chenjunwei
 * @desc
 * @date 2019/8/13
 */
public class NoticeDialogFragment extends BaseDialogFragment {

  private Announced mAnnounced;
  private X5WebView wvTask;
  private ProgressBar progressBar1;

  @Override
  protected View getContentView() {
    View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_notice_layout, null);
    LinearLayout ll_dialog_notice = view.findViewById(R.id.ll_dialog_notice);
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_dialog_notice.getLayoutParams();
    layoutParams.width = (int) (ScreenUtils.getScreenWidth(mContext) * 0.98);
    layoutParams.height = (int) (ScreenUtils.getScreenHeight(mContext) * 0.91);
    layoutParams.topMargin = 90;
    layoutParams.gravity = Gravity.BOTTOM;
    ll_dialog_notice.setLayoutParams(layoutParams);

    if (mAnnounced != null) {
      TextView tvTitle = view.findViewById(R.id.tv_title);
      wvTask = view.findViewById(R.id.web_view_notice_value);
      progressBar1 = view.findViewById(R.id.progressBar1);
      tvTitle.setText(mAnnounced.title);

      if (!TextUtils.isEmpty(mAnnounced.url)) {
        wvTask.loadUrl(mAnnounced.url);
      } else {
        wvTask.loadData(mAnnounced.value, "text/html; charset=UTF-8", "UTF-8");
      }
    }
    //监听网页的加载进度
    wvTask.setWebChromeClient(new WebChromeClient(){
      @Override
      public void onProgressChanged(WebView webView, int i) {
        super.onProgressChanged(webView, i);
        if (i < 100) {
          progressBar1.setVisibility(View.VISIBLE);
          progressBar1.setProgress(i);
        } else {
          progressBar1.setVisibility(View.INVISIBLE);
        }
      }
    });
    wvTask.addJavascriptInterface(new JavascriptInterface(), "AndroidWebView");
    ImageView iv_close_notice = view.findViewById(R.id.iv_close_notice);
    iv_close_notice.setOnClickListener(v -> getDialog().dismiss());
    return view;
  }

  public void setDialogContent(Announced announced) {
    mAnnounced = announced;
  }


  @Override
  public void onResume() {
    wvTask.onResume();
    wvTask.getSettings().setJavaScriptEnabled(true);
    super.onResume();
  }

  @Override
  public void onPause() {
    super.onPause();
    wvTask.onPause();
    wvTask.getSettings().setLightTouchEnabled(false);
  }

  //销毁 放置内存泄漏
  @Override
  public void onDestroy() {
    if (this.wvTask != null) {
      wvTask.destroy();
    }
    super.onDestroy();
  }
}
